package com.mz.ozy.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


/**
 * RecyclerView的适配器，提供点击及长按事件
 *
 * @param <T> 实体列表
 * @param <VH> 继承{@link }
 * @author gaobingbing
 */
public abstract class RecyclerViewAdapter<T,VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {

    protected Context mContext;

    protected List<T> dataList;

    protected LayoutInflater mInflater;

    private OnItemClickListener onItemClickListener;

    public RecyclerViewAdapter(Context mContext, List<T> dataList) {
        this.mContext = mContext;
        this.dataList = dataList;
        mInflater = LayoutInflater.from(this.mContext);
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.size();
    }

    public T getItem(int position) {
        return dataList == null ? null : dataList.get(position);
    }

    @NonNull
    @Override
    public abstract VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType);

    @Override
    public void onBindViewHolder(@NonNull final VH holder, final int position) {
        if(onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder.itemView,position,dataList.get(position));
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {
                    int position = holder.getLayoutPosition();
                    onItemClickListener.onItemLongClick(holder.itemView,position,dataList.get(position));
                    return false;
                }
            });
        }
        onBindHolder(holder,getItem(position),position);
    }

    public abstract void onBindHolder(VH holder,T dataItem,int position);

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public void add(int position,T t) {
        dataList.add(position,t);
        notifyItemInserted(position);
    }

    public void delete(int position) {
        dataList.remove(position);
        notifyItemRemoved(position);
    }

    public List<T> getDataList() {
        return dataList;
    }

    public void setDataList(List<T> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    protected String getString(int stringRes) {
        return mContext.getString(stringRes);
    }

    protected String getString(int stringRes, Object... formatArgs) {
        return mContext.getString(stringRes,formatArgs);
    }

    protected int getColor(int colorRes) {
        return mContext.getResources().getColor(colorRes);
    }

    protected Spanned getHtml(int stringRes) {
        return Html.fromHtml(getString(stringRes));
    }

    protected Spanned getHtml(int stringRes, Object... formatArgs) {
        return Html.fromHtml(getString(stringRes,formatArgs));
    }

    protected void startActivity(Intent mIntent) {
        mContext.startActivity(mIntent);
    }

    protected void startActivity(Class<? extends Activity> targetClazz) {
        mContext.startActivity(new Intent(mContext,targetClazz));
    }

    /**
     * RecyclerView 未提供点击及长按事件
     *
     * @param <T> 泛型，点击项实体
     */
    public interface OnItemClickListener<T> {

        void onItemClick(View view, int position, T dataItem);

        void onItemLongClick(View view, int position, T dataItem);

    }


}